﻿using System;
using System.Collections.Generic;

public static class IEnumerableExtensions
{
    //private static bool IsNumeric(ValueType value)
    //{
    //    bool isNumeric = false;
    //    if(value is sbyte)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is byte)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is short)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is ushort)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is int)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is uint)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is long)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is ulong)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is char)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is float)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is double)
    //    {
    //        isNumeric = true;
    //    }
    //    else if(value is decimal)
    //    {
    //        isNumeric = true;
    //    }
    //    return isNumeric;    
    //}

    public static T Sum<T>(this IEnumerable<T> collection)
    {
        if (collection == null)
        {
            throw new NullReferenceException("Collection is not referenced"); 
        }
        dynamic sum = 0;
        foreach (var item in collection)
        {
            sum += item;
        }
        return sum;
    }

    public static T Product<T>(this IEnumerable<T> collection)
    {
        if (collection == null)
        {
            throw new NullReferenceException("Collection is not referenced");
        }
        dynamic product = 1;
        foreach (var item in collection)
        {
            product *= item;
        }
        return product;
    }

    public static T Avarage<T>(this IEnumerable<T> collection)
    {
        if (collection == null)
        {
            throw new NullReferenceException("Collection is not referenced");
        }
        dynamic sum = 0;
        int count = 0;
        foreach (var item in collection)
        {
            sum += item;
            count++;
        }
        dynamic avarage = sum / count;
        return avarage;
    }

    public static T Min<T>(this IEnumerable<T> collection)
    {
        if (collection == null)
        {
            throw new NullReferenceException("Collection is not referenced");
        }
        bool assigned = false;
        dynamic min = default(T);
        foreach (var item in collection)
        {
            if (!assigned)
            {
                min = item;
                assigned = true;
            }
            else
            {
                if (item < min)
                {
                    min = item;
                }
            }
        }
        return min;
    }

    public static T Max<T>(this IEnumerable<T> collection)
    {
        if (collection == null)
        {
            throw new NullReferenceException("Collection is not referenced");
        }
        bool assigned = false;
        dynamic max = default(T);
        foreach (var item in collection)
        {
            if (!assigned)
            {
                max = item;
                assigned = true;
            }
            else
            {
                if (item > max)
                {
                    max = item;
                }
            }
        }
        return max;
    }
}

